<script type="text/javascript">
	function refreshCandidatesType()
	{
		idpere = document.getElementById('dossier_typePere').value;
		idtype = document.getElementById('dossier_type_id').value;
		idcategorie = document.getElementById('dossier_categorie_id').value;

		var xhrTypes = getXmlHttpRequest2();
		xhrTypes.onreadystatechange = function()
		{
			if(xhrTypes.readyState == 4 && xhrTypes.status == 200)
			{
				textTypes = xhrTypes.responseText;
				candidatesData = json_parse(textTypes);
				refreshSelect('dossier_typePere', candidatesData['peres'], null, candidatesData['pere']);
				refreshSelect('dossier_type_id', candidatesData['types'], null, candidatesData['type']);
				refreshSelect('dossier_categorie_id', candidatesData['categories'], null, candidatesData['categorie']);

				if(candidatesData['categorie']==0)
				{
					if(document.getElementById('justifUrg'))
					{
						document.getElementById('justifUrg').style.display = "none";
					}

					if(document.getElementById('dateDelai'))
					{
						document.getElementById('dateDelai').style.display = "none";
						document.getElementById('dossier_dateRealSouhaitee').value = '';
					}
				}

				if(candidatesData['categorie']!='')
				{
					calculeDates();
					if(document.getElementById('dossier_dateRealSouhaitee'))
					{
						if(document.getElementById('dossier_dateRealSouhaitee').value != '')
						{
							validatedates();
						}
					}
				}
			}
		};

		xhrTypes.open("POST","<?php print url_for('ajax/getTypesAndCategories'); ?>",true);
		xhrTypes.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrTypes.send("idpere="+idpere+"&idtype="+idtype+"&idcategorie="+idcategorie);
	}

	function calculeDates()
	{
		xhrDates = getXmlHttpRequest2();
		xhrDates.onreadystatechange = function()
		{
			if(xhrDates.readyState == 4 && xhrDates.status == 200)
			{
				xtext = xhrDates.responseText;
				datesData = json_parse(xtext);
				document.getElementById('dossier_delaiStandar').value = datesData['delaiStd'];
				document.getElementById('dossier_dateReference').value = datesData['dateStd'];
				
				if(document.getElementById('dossier_dateRealSouhaitee'))
				{
					document.getElementById('dossier_dateRealSouhaitee').value = datesData['dateStd'];
				}

				if(datesData['delais_min'])
				{
					document.getElementById('spanDelais_min').innerHTML = datesData['delais_min'];
				}
				else
				{
					document.getElementById('spanDelais_min').innerHTML = "0";
				}

				if(datesData['commentaire'])
				{
					document.getElementById('spanCommentaire').innerHTML = datesData['commentaire'];
				}
				else
				{
					document.getElementById('spanCommentaire').innerHTML = "";
				}

				if(datesData['dateobjMini'])
				{
					document.getElementById('dossier_delaisMini').value = datesData['dateobjMini'];
				}

				document.getElementById('spanDelaiStandar').innerHTML = datesData['delaiStd'];
				document.getElementById('spanDateReference').innerHTML = datesData['dateStd'];
				document.getElementById('dateDelai').style.display = "block";
				validatedates();
			}
		};

		xhrDates.open("POST","<?php print url_for('ajax/calculeDate'); ?>",true);
		xhrDates.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		idcat = document.getElementById('dossier_categorie_id').value;
		xhrDates.send("categorie="+idcat);
	}

	function validatedates()
	{
		if(document.getElementById('dossier_dateRealSouhaitee'))
		{
			var xDateSouhaitee = document.getElementById('dossier_dateRealSouhaitee');

			if (xDateSouhaitee.value == '__/__/____')
			{

			}
			else
			{
				if(verifdt(xDateSouhaitee.value))
				{
					var dDateSouhaitee = getFieldDate(xDateSouhaitee.value);
					var delaisMini = document.getElementById('dossier_delaisMini');
					var dateDelaisMini = getFieldDate(delaisMini.value);

					if(dateDelaisMini>=dDateSouhaitee)
					{
						alert('La date souhait\351e est ant\351rieure \340 la date minimale (date du jour + d\351lai minimum).');
						xDateSouhaitee.value = document.getElementById('dossier_dateReference').value;
						document.getElementById('justifUrg').style.display = "none";
						document.getElementById('dossier_urgent').value = 'false';
					}
					else
					{
						var xDateStd = document.getElementById('dossier_dateReference');
						if(xDateStd)
						{
							if(verifdt(xDateStd.value))
							{
								dDateSouhaitee = getFieldDate(xDateSouhaitee.value);
								dDqteStd = getFieldDate(xDateStd.value);

								if(dDateSouhaitee < dDqteStd)
								{
									document.getElementById('justifUrg').style.display = "block";
									document.getElementById('dossier_urgent').value = 'true';
								}
								else
								{
									document.getElementById('justifUrg').style.display = "none";
									document.getElementById('dossier_urgent').value = 'false';
								}
							}
							else
							{
								alert('Le format de la date n\'est pas valide.');
							}
						}
					}
				}
				else
				{
					alert('Le format de la date souhaitee n\'est pas valide.');
				}
			}
		}
	}

	function datePickerClosed(dateField){
		validatedates();
	}

	function setStandard()
	{
		document.getElementById('justifUrg').style.display = "none";
		document.getElementById('dossier_urgent').value = 'false';
		document.getElementById('dossier_dateRealSouhaitee').value = document.getElementById('dossier_dateReference').value;
		document.getElementById('dossier_justifurgence').value = "";
	}
</script>